package com.beaven.java.exercise;

import java.util.Stack;

/**
 * @author Beaven
 * @date 2018/7/22 21:47
 * <p>
 * 实现一个特殊的栈，在实现栈的基本功能上，再返回栈中最小元素的操作
 */
public class One {

    private Stack<Integer> stackData;
    private Stack<Integer> minStack;

    public One() {
        this.stackData = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(Integer value) {
        if (minStack.isEmpty()) {
            minStack.push(value);
        } else if (value < getMin()) {
            minStack.push(value);
        } else {
            minStack.push(getMin());
        }
        stackData.push(value);
    }

    public int pop() {
        if (stackData.isEmpty()) {
            throw new RuntimeException("stack is null");
        }
        minStack.pop();
        return stackData.pop();
    }

    public int getMin() {
        if (minStack.isEmpty()) {
            throw new RuntimeException("stack is null");
        }
        return minStack.peek();
    }
}
